package com.ksyzt.ksite2.client.column;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.IAppModule;
import com.ksyzt.gwt.client.mainframe.ModuleProperties;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;

public class ColumnManager extends MessageComposite implements IAppModule {

	private MessageHandler m_editor_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.UPDATE) {
				tree.initTree(m_userid, m_orgtype,
						OrgType.getOrgNameByType(m_orgtype), m_onlyone);
				editor.sureCanDelete();
			}

		}
	};
	private static ColumnManagerUiBinder uiBinder = GWT
			.create(ColumnManagerUiBinder.class);

	interface ColumnManagerUiBinder extends UiBinder<Widget, ColumnManager> {
	}

	public ColumnManager() {

		initWidget(uiBinder.createAndBindUi(this));

		tree.addMessageHandler(m_tree_handler);

		editor.addMessageHandler(m_editor_handler);
	}

	@UiField
	ColumnEditor editor;

	@UiField
	ColumnTree tree;

	MessageHandler m_tree_handler = new MessageHandler() {

		public void onMessage(Object sender, Integer message, Object value) {
			if (MessageEvent.CREATEROOT.equals(message)) {
				editor.edit(m_userid, m_orgtype, null, null);
			} else if (MessageEvent.ITEMCLICK.equals(message)) {
				TreeItem ti = (TreeItem) value;
				COLUMNObj o = (COLUMNObj) ti.getUserObject();

				COLUMNObj p = null;
				TreeItem tip = tree.getTreeItem(o.getPID());

				if (tip != null && tip.getUserObject() != null) {
					p = (COLUMNObj) tip.getUserObject();
				}
				editor.edit(m_userid, m_orgtype, p, o);
			}

		}
	};
	int m_userid;
	int m_orgtype;
	boolean m_onlyone;

	public void initData(int userid, int orgtype, boolean onlyone,
			boolean enabledDelete) {
		m_userid = userid;

		m_onlyone = onlyone;
		if (m_orgtype != orgtype) {
			m_orgtype = orgtype;
			tree.initTree(m_userid, m_orgtype,
					OrgType.getOrgNameByType(m_orgtype), m_onlyone);
		}
		editor.clearData();
		editor.EnabledDelete(enabledDelete);
		editor.enabledEditor(false);
	}

	HandlerRegistration handler;
	public static String KEY_ORGTYPE = "key_orgtype";
	public static String KEY_ONLYONE = "key_only";
	public static String KEY_ENABLE_DELETE = "key_enable_delete";

	@Override
	public void onInitialize(AppData application, ModuleProperties pros) {
		handler = this.addMessageHandler(application.messageHandler);

		if (pros != null) {
			int userid = application.user.id;
			int orgtype = pros.getInt(KEY_ORGTYPE, OrgType.OT_ORGNIZATION);
			boolean onlyone = pros.getBoolean(KEY_ONLYONE, true);
			boolean enabledelete = pros.getBoolean(KEY_ENABLE_DELETE, false);
			initData(userid, orgtype, onlyone, enabledelete);
		}
	}

	@Override
	public void onUnInitialize() {
		handler.removeHandler();
	}

	@Override
	public Widget getRootWidget() {
		return this;
	}

}
